package com.example.sqlist03;

import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.service.autofill.OnClickAction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

public class main_Adapter extends RecyclerView.Adapter<main_Adapter.adapter_mylist> {
    private List<Student> list;
    OnClickInterface clickInterface;
    interface OnClickInterface{
        public void onclick(View view,int position);
    }
    public void setClickInterface(OnClickInterface clickInterface){
        this.clickInterface=clickInterface;
    }
    public main_Adapter(List<Student> list) {
        this.list = list;
    }
    @NonNull
    @Override
    public adapter_mylist onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new adapter_mylist(LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter,null));
    }

    @Override
    public void onBindViewHolder(@NonNull adapter_mylist holder, int position) {
        Student student=list.get(position);
        holder._id.setText(""+student.get_id());
        holder.name.setText(""+student.getName());
        holder.age.setText(""+student.getAge());
        holder.button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                   if (clickInterface!=null){
                        clickInterface.onclick(v,position);
                   }
            }
        });
        holder.button_2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (clickInterface!=null){
                    clickInterface.onclick(v,position);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }
    public class adapter_mylist extends RecyclerView.ViewHolder {
        private Button button,button_2;
        private TextView _id, name, age;
        public adapter_mylist(@NonNull View itemView) {
            super(itemView);
            _id=itemView.findViewById(R.id._id);
            name=itemView.findViewById(R.id.name);
            age=itemView.findViewById(R.id.age);
            button=itemView.findViewById(R.id.clos);
            button_2=itemView.findViewById(R.id.gengxin);
        }
    }
}
